package net.sudot.chess.business.dao;

import net.sudot.chess.business.model.User;
import net.sudot.commons.pagination.Page;
import net.sudot.commons.pagination.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.TypedQuery;

/**
 * 用户管理
 *
 * @author tangjialin on 2018-03-30.
 */
@Repository("userDao")
public class UserDao extends BaseDao<User> {

    /**
     * 列表
     *
     * @param pageable 分页参数
     * @return
     */
    public Page<User> findList(Pageable pageable) {
        String hql = "SELECT _user FROM User AS _user ORDER BY _user.username ASC";
        return null;
    }

    /**
     * 通过用户名获取用户信息
     *
     * @param username 账号
     * @return 不存在返回null
     */
    public User findByUsername(String username) {
        String hql = "SELECT _user FROM User AS _user WHERE _user.deleted=FALSE AND _user.username=:username";
        TypedQuery<User> query = entityManager.createQuery(hql, entityClass);
        query.setParameter("username", username);
        return uniqueResult(query);
    }

}
